import Reflux from 'reflux';
import Actions from "./Actions";
import update from 'react-addons-update';
import {Control} from "react-keeper";
import DB from '../../../../../app/db';
import StorageUtils from "../../../../../app/storage";
import {getInfoByOpenId, RwCode} from "../../const/Const";
import Utils from "../../../../../utils/index";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    userInfo: {},
    rw: {
      CAST01: [],
      CAST02: [],
      CAST03: [],
      CAST04: []
    },
    court: {}
  },

  init() {
    // this.initUserInfo();
  },

  initUserInfo() {
    this.data.userInfo = update(this.data.userInfo, {$set: StorageUtils.getUser()});
    this.updateComponent();
  },

  getFromArray(array, keyValue) {
    let rtn = [];
    if (Utils.arrayIsNotEmpty(array) && Utils.arrayIsNotEmpty(keyValue)) {
      array.map(obj => {
        let isFind = true;
        keyValue.map(kv => {
          if (!(obj.hasOwnProperty(kv.key) && obj[kv.key] === kv.value)) {
            isFind = false;
          }
        });
        if (isFind) {
          rtn.push(obj);
        }
      })
    }
    return rtn;
  },

  // 获取投票任务列表
  getYwhTp(courtId) {
    let t = this;
    t.initUserInfo();
    // 更新门牌号信息
    getInfoByOpenId(t.data.userInfo.openId);
    let tpRw = {
      CAST01: [],
      CAST02: [],
      CAST03: [],
      CAST04: []
    };
    DB.WechatAPI.getCourtById({id: courtId}).then(
      function (court) {
        t.data.court = update(t.data.court, {$set: court});
        t.updateComponent();

        DB.ywh.getYwhTp({courtId: courtId, proprietorId: t.data.userInfo.proprietorId}).then(
          function (content) {
            if (content && content.length > 0) {
              let myDoors = StorageUtils.getStorageItem('doors');
              content.map(rw => {

                let filter = [];
                switch (rw.rwCode) {
                  case RwCode.CAST01:
                    filter = [
                      {key: 'courtId', value: rw.courtId},
                      {key: 'building', value: rw.building},
                      {key: 'unit', value: rw.unit},
                      {key: 'rightUnit', value: true}
                    ];
                    break;
                  case RwCode.CAST02:
                    filter = [
                      {key: 'courtId', value: rw.courtId},
                      {key: 'building', value: rw.building},
                      {key: 'rightBuilding', value: true}
                    ];
                    break;
                  case RwCode.CAST03:
                    filter = [
                      {key: 'courtId', value: rw.courtId},
                      {key: 'rightMember', value: true}
                    ];
                    break;
                  case RwCode.CAST04:
                    filter = [
                      {key: 'courtId', value: rw.courtId},
                      {key: 'rightHead', value: true}
                    ];
                    break;
                  default:
                    break;
                }
                let door = t.getFromArray(myDoors, filter);
                let votedDoorplateId = '';
                if (door && door.length > 0) {
                  switch (rw.rwCode) {
                    case RwCode.CAST01:
                      votedDoorplateId = door[0].unitVoteDoorplateId;
                      break;
                    case RwCode.CAST02:
                      votedDoorplateId = door[0].buildingVoteDoorplateId;
                      break;
                    case RwCode.CAST03:
                      votedDoorplateId = door[0].memberVoteDoorplateId;
                      break;
                    case RwCode.CAST04:
                      votedDoorplateId = door[0].headVoteDoorplateId;
                      break;
                    default:
                      break;
                  }
                }
                let obj = {
                  rwId: rw.id,
                  rwName: rw.rwName,
                  rwCode: rw.rwCode,
                  startTime: rw.startTime,
                  endTime: rw.endTime,
                  courtId: rw.courtId,
                  building: rw.building,
                  unit: rw.unit,
                  isVoted: door && door.length > 0,
                  votedDoorplateId: votedDoorplateId
                };
                switch (rw.rwCode) {
                  case RwCode.CAST01:
                    tpRw.CAST01.push(obj);
                    break;
                  case RwCode.CAST02:
                    tpRw.CAST02.push(obj);
                    break;
                  case RwCode.CAST03:
                    tpRw.CAST03.push(obj);
                    break;
                  case RwCode.CAST04:
                    tpRw.CAST04.push(obj);
                    break;
                  default:
                    break;
                }
              });
              t.data.rw = update(t.data.rw, {$set: tpRw});
              t.updateComponent();
            }
          },
          function (error) {
            console.log(error);
          }
        );
      },
      function (error) {
        console.log(error);
      }
    );
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState() {
    return this.data;
  }
});
